package com.unissoft.ordersystem.dataaccess.common;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 服务器查询条件 继承于BaseCriteria  仅做示例用 每一个实体类都会根据示例有一个自己的
 * 
 * @author aaron
 * 
 */
public class EntityCriteria extends BaseCriteria {

	/**
	 * 映射数据库查询时的别名
	 */
	private static Map<String, String> columnKeyMap;

	static {
		columnKeyMap = new HashMap<String, String>();
		columnKeyMap.put("sid", "sid");
	}

	private String getMapValue(String columnKey) {
		if (columnKeyMap.containsKey(columnKey)) {
			return columnKeyMap.get(columnKey);
		}
		return null;
	}

	@Override
    public void addCriterionEqual(String key, Object value) {
        String conditionColumn = this.getMapValue(key);
        this.addCriterion(conditionColumn, ConditionOperate.EQUAL, value, key);
    }

    @Override
    public void addCriterionLike(String key, Object value) {
        String conditionColumn = this.getMapValue(key);
        this.addCriterion(conditionColumn, ConditionOperate.LIKE, "%" + value
                + "%", key);
    }

    @Override
    public void addCriterionIn(String key, List<?> value) {
        String conditionColumn = this.getMapValue(key);
        this.addCriterion(conditionColumn, ConditionOperate.IN, value, key);
    }

    @Override
    public void addCriterionBetween(String key, Object value1, Object value2) {
        String conditionColumn = this.getMapValue(key);
        this.addCriterion(conditionColumn, ConditionOperate.BETWEEN, value1,
                value2, key);
    }

    @Override
    public void addCriterionGE(String key, Object value) {
        String conditionColumn = this.getMapValue(key);
        this.addCriterion(conditionColumn, ConditionOperate.GE, value, key);
    }

    @Override
    public void addCriterionLE(String key, Object value) {
        String conditionColumn = this.getMapValue(key);
        this.addCriterion(conditionColumn, ConditionOperate.LE, value, key);
    }
    
    @Override
    public void addCriterionNotEqual(String key, Object value) {
        String conditionColumn = this.getMapValue(key);
        this.addCriterion(conditionColumn, ConditionOperate.NOT_EQUAL, value, key);
    }

	/**
	 * 将客户端查询条件转为服务器查询条件
	 * 
	 * @param entityCriteria
	 * @param criteria
	 * @author aaron
	 * 
	 */
	public void convertOrganizationCriteria(
			EntityCriteria entityCriteria, Criteria criteria) {
		Map<String, String> equalMap = null;
		Map<String, String> likeMap = null;
		Map<String, List<String>> inMap = null;
		Map<String, BetweenValueObject> betweenMap = null;

		if (null != criteria) {
			equalMap = criteria.getEqualMap();
			likeMap = criteria.getLikeMap();
			inMap = criteria.getInMap();
			betweenMap = criteria.getBetweenMap();

			// Set sort
			if (null != criteria.getOrderByClause()
					&& !"".equals(criteria.getOrderByClause())) {
				entityCriteria.setOrderByClause(getMapValue(criteria
						.getOrderByClause()));
				entityCriteria.setSortMark(SortMark.valueOf(criteria
						.getSortMark().name()));
			}
		}

		// Iterates equalMap, and addCriterionEqual
		if (null != equalMap) {
			Set<Map.Entry<String, String>> equalSet = equalMap.entrySet();
			Iterator<Map.Entry<String, String>> equalIt = equalSet.iterator();
			while (equalIt.hasNext()) {
				Map.Entry<String, String> entry = equalIt.next();
				if (null != entry.getValue() && !"".equals(entry.getValue())) {
					entityCriteria.addCriterionEqual(entry.getKey(),
							entry.getValue());
				}
			}
		}

		// Iterates likeMap, and addCriterionLike
		if (null != likeMap) {
			Set<Map.Entry<String, String>> likeSet = likeMap.entrySet();
			Iterator<Map.Entry<String, String>> likeIt = likeSet.iterator();
			while (likeIt.hasNext()) {
				Map.Entry<String, String> entry = likeIt.next();
				if (null != entry.getValue() && !"".equals(entry.getValue())) {
					entityCriteria.addCriterionLike(entry.getKey(),
							entry.getValue());
				}
			}
		}

		// Iterates inMap, and addCriterionIn
		if (null != inMap) {
			Set<Map.Entry<String, List<String>>> inSet = inMap.entrySet();
			Iterator<Map.Entry<String, List<String>>> inIt = inSet.iterator();
			while (inIt.hasNext()) {
				Map.Entry<String, List<String>> entry = inIt.next();
				if (null != entry.getValue() && entry.getValue().size() != 0) {
					entityCriteria.addCriterionIn(entry.getKey(),
							entry.getValue());
				}
			}
		}

		// Iterates betweenMap, and addCriterionBetween
		if (null != betweenMap) {
			Set<Map.Entry<String, BetweenValueObject>> betweenSet = betweenMap
					.entrySet();
			Iterator<Map.Entry<String, BetweenValueObject>> betweenIt = betweenSet
					.iterator();
			while (betweenIt.hasNext()) {
				Map.Entry<String, BetweenValueObject> entry = betweenIt.next();
				if (null != entry.getValue()) {
					if (null != entry.getValue().getFirstValue()
							&& !"".equals(entry.getValue().getFirstValue())) {
						entityCriteria.addCriterionGE(entry.getKey(), entry
								.getValue().getFirstValue());
					}
					if (null != entry.getValue().getSecondValue()
							&& !"".equals(entry.getValue().getSecondValue())) {
						entityCriteria.addCriterionLE(entry.getKey(), entry
								.getValue().getSecondValue());
					}
				}
			}
		}

	}
}
